$(function () {
    $("#jqGrid").jqGrid({
        url: 'ds/page',
        datatype: "json",
        colModel: [
            { label: '主数据源', name: 'primary', width: 15 ,formatter:function (value) {
                    return value ? "<font color='red'>是</font>" : "<font color='green'>否</font>";;
                }},
            { label: '数据源名称', name: 'ds', width: 20, key: true },
            { label: '类型', name: 'dbType', width: 20,},
            { label: '账号', name: 'username', width: 30},
            { label: '密码', name: 'password', width: 30 },
            { label: '主机', name: 'host', width: 40 },
            { label: '端口', name: 'port', width: 10 },
            { label: '数据库', name: 'dbName', width: 20 },
            { label: '创建时间', name: 'createTime', width: 30 }
        ],
        loadComplete: function (data) {
            if(data.code != 0){
                window.alert(data.msg)
            }
        },
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50,100,200],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            'author':'wtf'
        },
        ds: '',
        dialogVisible: false,
        addDs:{
            dbType:null,
            ds:null,
            host:null,
            port:null,
            username:null,
            password:null,
            dbName:null
        },
        dbTypes:[
            'MYSQL',
            'POSTGRE_SQL',
            'ORACLE',
        ]
    },
    methods: {
        query: function () {
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'ds': vm.ds,'author': vm.q.author},
                page:1
            }).trigger("reloadGrid");
        },
        createDs:function () {
            httpPost(localUrl + "/ds/add",vm.addDs,"创建数据源成功");
            location.reload()
        },
        testCon:function () {
            httpPost(localUrl + "/ds/testConnect",vm.addDs,"连接成功");
        },
        clear:function () {
            vm.ds = ''
            this.query()
        },
        del:function () {
            var dss = []
            for (var rowData of getSelectedRowsData()) {
                if(rowData.primary){
                    window.alert("主数据源不允许删除")
                }
                dss.push(rowData.ds)
            }
            if(dss.length == 0){
                window.alert("请选择需要删除的数据源")
            }
            var delParam = {
                ds:dss
            }
            httpPost(localUrl + "/ds/del",delParam,"删除数据源成功");
           location.reload()
        }
    }
});

